package com.hamu.controller;

import com.hamu.pojo.Article;
import com.hamu.pojo.PageBean;
import com.hamu.pojo.Result;
import com.hamu.service.ArticleService;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    //添加文章
    @PostMapping
    public Result add(@RequestBody @Validated Article article){
        articleService.add(article);
        return Result.success();
    }

    //文章列表
    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum,
            Integer pageSize,
            //非必须
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state,
            @RequestParam(required = false) List<String> tagNames,
            @RequestParam(required = false) List<String> tagIds
    ){
        PageBean<Article>pb = articleService.list(pageNum,pageSize,categoryId,state,tagNames,tagIds);
        return Result.success(pb);
    }

    //文章列表（管理员
    @GetMapping("/all")
    public Result<PageBean<Article>> listAll(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state,
            @RequestParam(required = false) List<String> tagNames,
            @RequestParam(required = false) List<String> tagIds
    ) {
        PageBean<Article> pb = articleService.listAll(pageNum,pageSize,categoryId,state,tagNames,tagIds);
        return Result.success(pb);
    }

    //搜索文章
    @GetMapping("/search")
    public Result<PageBean<Article>> searchArticles(
            Integer pageNum,
            Integer pageSize,
            //非必须
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state,
            @RequestParam(required = false) List<String> tagNames,
            @RequestParam(required = false) List<String> tagIds,
            @RequestParam(required = false) String query){
        PageBean<Article> pb = articleService.searchList(pageNum, pageSize,categoryId,state,tagNames,tagIds,query);
        return Result.success(pb);
    }

    //文章详情
    @GetMapping("/detail")
    public Result<Article> detail(@NotNull Integer id) {
        Article article = articleService.findById(id);
        if (article == null) {
            return Result.error("该文章不存在");
        }
        return Result.success(article);
    }



    //更新文章内容
    @PutMapping
    public Result update(@RequestBody @Validated Article article) {
        if (articleService.findById(article.getId()) == null) {
            return Result.error("该文章不存在");
        }
        articleService.update(article);
        return Result.success();
    }

    //删除文章
    @DeleteMapping
    public Result delete(@NotNull Integer id){
        articleService.delete(id);
        return Result.success();
    }

}
